package org.luckystrophe.snowtoms.manager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.luckystrophe.snowtoms.R;
import org.luckystrophe.snowtoms.pref.UserPreference_;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.res.StringArrayRes;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.googlecode.androidannotations.api.Scope;

@EBean(scope = Scope.Singleton)
public class SnowtomsUtils {

	@RootContext
	Context context;

	@StringArrayRes(R.array.setLocation)
	String[] location;

	@StringArrayRes(R.array.seoul)
	String[] fixedLocation;

	@Pref
	UserPreference_ pref;

	public final static int LOCATION = 1;

	public final static int FIXED_LOCATION = 2;

	String[] finalLocationArray;

	public static Date getDate(String stringDate) {
		Date convertedDate = new Date();
		try {
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.KOREA);
			convertedDate = sdf.parse(stringDate);
			calendar.setTime(convertedDate);
			calendar.add(Calendar.HOUR_OF_DAY, +9);
			convertedDate = calendar.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return convertedDate;
	}

	public void setLocationDialog(final Context context, boolean isStrictSetLocation, OnCancelListener cancelListener,
			OnClickListener selectListener) {
		finalLocationArray = isStrictSetLocation ? fixedLocation : location;
		AlertDialog.Builder listBuilder = new AlertDialog.Builder(context);
		listBuilder.setTitle("지역구를 선택해 주세요.");
		listBuilder.setOnCancelListener(cancelListener);
		if (selectListener == null) {
			listBuilder.setItems(finalLocationArray, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					Toast.makeText(context, finalLocationArray[item], Toast.LENGTH_SHORT).show();
					if (item == 0 && finalLocationArray.length == 26) {
						pref.location().remove();
						return;
					}
					pref.location().put(finalLocationArray[item]);
				}
			});
		} else {
			listBuilder.setItems(finalLocationArray, selectListener);
		}
		AlertDialog alertList = listBuilder.create();
		alertList.show();
	}
}
